package com.second.ssyt_test.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;

import com.second.ssyt_test.common.Constant;

/**

 * 时间操作工具类

 * 

 * @author Hsu.

 * @since 2016年3月3日

 */
public class DateUtil {

	public static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";

	private DateUtil() {

	}

	/**

	 * Date -> String

	 * 

	 * @param date

	 * @param pattern

	 * @return

	 */
	public static String format(Date date) {
		return format(date, DEFAULT_PATTERN);
	}

	public static String format(Date date, String pattern) {
		if (date == null || StringUtils.isBlank(pattern)) {
			return "";
		}

		return new SimpleDateFormat(pattern).format(date);
	}

	/**

	 * String -> Date

	 * 

	 * @param date

	 * @param pattern

	 * @return

	 * @throws ParseException

	 */
	public static Date parse(String dateStr) throws ParseException {
		return parse(dateStr, DEFAULT_PATTERN);
	}

	public static Date parse(String dateStr, String pattern)
			throws ParseException {
		Validate.notBlank(dateStr, "date str" + Constant.VALIDATE_NOT_BLANK);
		Validate.notBlank(pattern,
				"date pattern" + Constant.VALIDATE_NOT_BLANK);

		return new SimpleDateFormat(pattern).parse(dateStr);
	}

}
